package sy1_UML.association.aggregation.way1;
import java.util.ArrayList;

/**
 * @author wustwzx
 * 聚合关系示例：Company（整体类）与Employee（部分类）
 * 要点：成员对象可以脱离整体对象而独立存在。
 *      例如，部分类对象通过部分类的构造器创建（聚合的一种使用方式）。
 */

import java.util.List;

class Company {   //整体类
	private String companyName;
	private List<Employee> employees;  //Company关联Employee
	
	public Company(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyName() {
		return companyName;
	}

	public List<Employee> getEmployees() {
		return employees;
	}
}

class Employee { // 部分类
	private String name;

	public Employee(String name) { // 有参构造器
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

public class CompanyAndEmployee {

	public static void main(String[] args) throws Exception{
		
		//通过部分类的构造器创建部分类对象
		Employee employee1 = new Employee("张三");
		Employee employee2 = new Employee("李四");
		
		Company company = new Company("XXX");
		List<Employee> employees = company.getEmployees();
		employees=new ArrayList<Employee>();  //实例化部分对象
		employees.add(employee1);
		employees.add(employee2);
		
		System.out.println(company.getCompanyName()+"的员工如下：");
		for(Employee em:employees) {
			System.out.println(em.getName());
		}
	}
}
